
import { _decorator, Component, Node, AudioSource, AudioClip, game, Game, Toggle } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('SoundEffect')
export class SoundEffect extends Component {
    // 背景音乐,启动后编辑器设置自动循环播放
    @property(AudioSource)
    bgAudio: AudioSource = null!;
    // 长音乐资源
    @property(AudioSource)
    audioSourceS: AudioSource[] = [];
    // 短音效
    @property(AudioClip)
    audioClipS: AudioClip[] = [];

    @property(Toggle)
    musicToggle: Toggle = null!;

    static instance: SoundEffect = null;
    onLoad() {
        SoundEffect.instance = this;
    }

    static isOpenMusic=true;

    start() {

        game.on(Game.EVENT_HIDE, this.onEventHide, this);
        game.on(Game.EVENT_SHOW, this.onEventShow, this);

        if (SoundEffect.isOpenMusic) {
            this.musicToggle.isChecked=true;
        }else
        {
            this.musicToggle.isChecked=false;
        }
    }



    onEventHide() {
        //进入后台手动暂停音乐
        // console.log("后台");
        if (this.bgAudio) {
            // console.log("暂停音乐");
            this.bgAudio.pause();
        }

    }

    ClickMusicToogle()
    {
        if (SoundEffect.isOpenMusic) {
            SoundEffect.isOpenMusic=false;
            if (this.bgAudio) {
                this.bgAudio.pause();
            }
        }else
        {
            SoundEffect.isOpenMusic=true;
            if (this.bgAudio) {
                this.bgAudio.play();
            }
        }
    }

    onEventShow() {
        //进入前台手动开启音乐
        // console.log("前台");
        if (!this.bgAudio.playing) {
            // console.log("开启音乐");
            this.bgAudio.play();
        }

    }
    onDestroy() {
        game.off(Game.EVENT_HIDE, this.onEventHide, this);
        game.off(Game.EVENT_SHOW, this.onEventShow, this);

    }

    /**
     * 只播放一个，不重新开始播放
     * @param name 名字
     * @param loop 是否循环
     * @param volume 音量0.0-1.0
     */
    playAudioSourceOnlyOne(index: number = 0, loop: boolean = false, volume: any = 1.0) {
        if (!this.audioSourceS[index]) {
            return;
        }
        if (!this.audioSourceS[index].playing) {
            this.audioSourceS[index].loop = loop;
            this.audioSourceS[index].play();
        }
    }


    /**
     * 只播放一个，不重新开始播放
     * @param name 名字
     * @param loop 是否循环
     * @param volume 音量0.0-1.0
     */
    playAudioSourceNameOnlyOne(name: string, loop: boolean = false, volume: any = 1.0) {
        for (let index = 0; index < this.audioSourceS.length; index++) {
            const element = this.audioSourceS[index];

            if (element.node.name == name) {
                if (!element.playing) {
                    element.loop = loop;
                    element.play();
                }
                return;
            }
        }
    }

    /**
     * 播放
     * @param name 名字
     * @param loop 是否循环
     * @param volume 音量0.0-1.0
     */
    playAudioSourceName(name: string, loop: boolean = true, volume: any = 1.0) {

        for (let index = 0; index < this.audioSourceS.length; index++) {
            const element = this.audioSourceS[index];

            if (element.node.name == name) {
                element.loop = loop;
                element.play();
                return;
            }
        }
    }
    // 暂停
    pauseAudioSourceName(name: string) {
        for (let index = 0; index < this.audioSourceS.length; index++) {
            const element = this.audioSourceS[index];
            if (element.node.name == name) {
                if (element.playing) {
                    element.pause();
                }
                return;
            }
        }
    }
    // 停止
    stopAudioSourceName(name: string) {
        for (let index = 0; index < this.audioSourceS.length; index++) {
            const element = this.audioSourceS[index];
            if (element.node.name == name) {
                if (element.playing) {
                    element.stop();
                }
                return;
            }
        }
    }
    // 音效播放时间短，同时播放的数量多,一次性播放操作，播放后的声音没法暂停或停止播放，也没法监听播放结束的事件回调。
    playOneShotName(name: string, volume: any = 1.0) {
        for (let index = 0; index < this.audioClipS.length; index++) {
            const element = this.audioClipS[index];
            if (element.name == name) {
                if (SoundEffect.isOpenMusic) {
                    this.bgAudio.playOneShot(element, volume);
                }
                return;

            }
        }
    }

}